package ru.yermak.bookkeeping.ui;

import ru.yermak.bookkeeping.common.DataModel;
import ru.yermak.bookkeeping.model.Selectable;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * User: harrier
 * Date: Nov 4, 2008
 */
public abstract class DataTableModel<T extends Selectable> extends AbstractTableModel implements DataModel {

    protected List<T> data;
    protected int rowCount;


    public abstract void load();

    public abstract void clean();


    public Object get(int row) {
        if (data == null) {
            return null;
        }
        if (row >= rowCount) {
            return null;
        }
        return data.get(row);
    }

    public Integer getRowById(Integer selectedId) {
        if (data == null) {
            return null;
        }
        for (int i = 0; i < rowCount; i++) {
            T t = data.get(i);
            if (t.getId().equals(selectedId)) {
                return i;
            }
        }
        return null;
    }

    public int getRowCount() {
        return rowCount;
    }

    protected List<T> getData() {
        return data;
    }

    protected void setData(List<T> data) {
        this.data = data;
        rowCount = data.size();
    }
}
